﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Portal.Cms.Model;
using Portal.Database.Service;
using Portal.Web.Utility;
using Portal.Core;

namespace Portal.Cms.Bussiness
{
    public class DocFieldProcessBll
    {
        public static DocFieldCls CreateDocFieldModel(string SiteId, string DocFieldId)
        {
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            DataSet dsResult =
                    DBService.GetDataSet(null, "select * from TableDocField " +
                                               "     where DocFieldId=@DocFieldId order by SortIndex",
                    new DbParam[] 
                    { 
                        new DbParam("DocFieldId", DocFieldId),
                    });
            if (dsResult.Tables[0].Rows.Count == 0)
            {
                return null;
            }
            DocFieldCls ODocField = DocFieldParser.ParseFromDataRow(dsResult.Tables[0].Rows[0]);
            dsResult.Clear();
            dsResult.Dispose();
            return ODocField;
        }



        public static DocFieldCls[] ReadingDocField(string SiteId, string GroupDataId)
        {
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            DataSet dsResult =
                    DBService.GetDataSet(null, "select * from TableDocField where frkGroupDataId=@frkGroupDataId order by SortIndex",
                    new DbParam[] { new DbParam("frkGroupDataId", GroupDataId) });
            if (dsResult.Tables[0].Rows.Count == 0)
            {
                return new DocFieldCls[0];
            }
            DocFieldCls[] DocField = DocFieldParser.ParseFromDataTable(dsResult.Tables[0]);
            dsResult.Clear();
            dsResult.Dispose();
            return DocField;
        }


        public static void AddDocField(string SiteId,string GroupDataId, string DocFieldName, string SortIndex)
        {
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
            string DocFieldId = System.Guid.NewGuid().ToString();
            string AssetCode = WebSessionUtility.GetCurrentLoginUser().AssetCode;
            string DataLanguage = WebConfig.GetDataLanguage(OSiteParam);
            
            DBService.Insert(null, "TableDocField",
                    new DbParam[] 
                    { 
                        new DbParam("DocFieldId", DocFieldId),
                        new DbParam("frkGroupDataId", GroupDataId),
                        new DbParam("DocFieldName", DocFieldName),
                        new DbParam("SortIndex", SortIndex),
                        new DbParam("AssetCode", AssetCode),
                        new DbParam("DataLanguage", DataLanguage),
                    });
            SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).AddSysLogProcess(SiteId,
                WebSessionUtility.GetCurrentLoginUser().AssetCode,
                "Dictionary", "Thêm mới lĩnh vực " + DocFieldName,
                WebSessionUtility.GetCurrentLoginUser().UserId,
                WebSessionUtility.GetCurrentLoginUser().LoginName, 
                WebSessionUtility.GetCurrentLoginUser().FullName);
        }

        
        public static void SaveDocField(string SiteId, string DocFieldId, string DocFieldName,string SortIndex)
        {
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            DBService.Update(null, "TableDocField", "DocFieldId", DocFieldId,
                    new DbParam[] 
                    { 
                        new DbParam("DocFieldName", DocFieldName),
                        new DbParam("SortIndex", SortIndex),
                    });
            SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).AddSysLogProcess(SiteId,
                WebSessionUtility.GetCurrentLoginUser().AssetCode,
                "Dictionary", "Sửa chữa lĩnh vực " + DocFieldName, 
                WebSessionUtility.GetCurrentLoginUser().UserId,
                WebSessionUtility.GetCurrentLoginUser().LoginName, 
                WebSessionUtility.GetCurrentLoginUser().FullName);
        }


        public static void DeleteDocField(string SiteId, string DocFieldId)
        {
            DocFieldCls ODocField = DocFieldProcessBll.CreateDocFieldModel(SiteId, DocFieldId);
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            string Query =
                " Delete from TableDocField where DocFieldId=@DocFieldId; ";
            DBService.ExecuteNonQuery(null, Query,
                    new DbParam[] 
                    { 
                        new DbParam("DocFieldId", DocFieldId),
                    });
            SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).AddSysLogProcess(SiteId,
                WebSessionUtility.GetCurrentLoginUser().AssetCode,
                "Dictionary", "Xóa lĩnh vực " + ODocField.DocFieldName, 
                WebSessionUtility.GetCurrentLoginUser().UserId,
                WebSessionUtility.GetCurrentLoginUser().LoginName, 
                WebSessionUtility.GetCurrentLoginUser().FullName);
        }
    }
}
